package com.key.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.key.common.R;
import com.key.common.dto.AssetFCAdvDTO;
import com.key.common.dto.AssetFCDTO;
import com.key.common.vo.AssetFCVO;
import com.key.common.vo.AssetUpdateFCVO;
import com.key.common.vo.ChangeAssetsListVO;
import com.key.common.vo.FCAssetsVO;
import com.key.entity.AssetChange;
import com.key.entity.AssetFinanceChange;
import com.key.entity.Company;
import com.key.mapper.AssetChangeMapper;
import com.key.mapper.AssetFinanceChangeMapper;
import com.key.mapper.ShowBackMapper;
import com.key.service.AssetFinanceChangeService;
import com.key.utils.ExcelExportHelper;
import com.key.utils.ShowBackUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
*
 * @description:财务信息变更相关业务
 * @author: jianjiawei
 * @date: 2023/2/21 16:07
 **/
@Service
public class AssetFinanceChangeServiceImpl implements AssetFinanceChangeService {

    @Autowired
    AssetFinanceChangeMapper afcMapper;
    @Autowired
    private AssetChangeMapper acMapper;
    @Autowired
    private ShowBackUtils showBackUtils;
    @Autowired
    private ShowBackMapper showBackMapper;
    /*
    *
     * @description:新建财务信息变更的表单数据回显
     * @author: jianjiawei
     * @date: 2023/2/21 16:07
     **/
    @Override
    public R showAssetsFinanceChangeNew() {
        // 生成变更单号
        AssetFinanceChange afc = afcMapper.selectLastOne();
        Integer num = 10000001 + afc.getId();
        String changeNo = "fc" + num;
        List<String> companies = showBackUtils.showCompanies();
        Map map = new HashMap();
        map.put("changeNo",changeNo);
        map.put("companies",companies);

        String data = JSONObject.toJSONString(map);
        R r = new R();
        r.setCode(200);
        r.setMsg("success");
        r.setData(data);
        return r;
    }

    /*
    *
     * @description: 财务信息变更保存
     * @author: jianjiawei
     * @date: 2023/2/21 18:15
     **/
    @Override
    @Transactional
    public R saveAssetsFinanceChange(AssetFCDTO fcDTO) {
        int row = afcMapper.insertAssetFinanceChange(fcDTO);
        R r = new R();
        if (row > 0) {
            r.setCode(200);
            r.setMsg("success");
        } else {
            r.setCode(500);
            r.setMsg("添加失败");
        }
        return r;
    }

    /*
    *
     * @description:查询财务信息变更列表
     * @author: jianjiawei
     * @date: 2023/2/21 18:30
     **/
    @Override
    public R showAssetsFinanceChangeList(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<AssetFCVO> assetFCVOS = afcMapper.selectAssetFinanceChangeList();
        if (assetFCVOS != null & assetFCVOS.size() > 0){
            for (AssetFCVO assetFCVO : assetFCVOS) {
                assetFCVO.setAssetsList(this.showChangeAssetsList(assetFCVO.getId()));
            }
        }
        R r = new R();
        if (assetFCVOS != null){
            String data = JSONObject.toJSONString(assetFCVOS);
            r.setCode(200);
            r.setMsg("success");
            r.setData(data);
        }else {
            r.setCode(200);
            r.setMsg("无数据");
        }
        return r;
    }

    /*
    *
     * @description:修改财务信息变更的表单数据回显
     * @author: jianjiawei
     * @date: 2023/2/21 18:50
     **/
    @Override
    public R showAssetsFinanceChangeUpdate(Integer id) {
        AssetUpdateFCVO updateFCVO = afcMapper.selectAssetFinanceChangeById(id);
        R r = new R();
        if (updateFCVO != null){
            String data = JSONObject.toJSONString(updateFCVO);
            r.setCode(200);
            r.setMsg("success");
            r.setData(data);
        }else {
            r.setCode(201);
            r.setMsg("Id有误");
        }

        return r;
    }

    /*
    *
     * @description:修改财务信息变更清单
     * @author: jianjiawei
     * @date: 2023/2/21 19:12
     **/
    @Override
    @Transactional
    public R updateAssetsFinanceChange(AssetFCDTO fcDTO) {
        System.out.println(fcDTO);
        int row = afcMapper.updateAssetFinanceChange(fcDTO);
        if (row > 0){
            //获取公司ID
            List<Company> companies = showBackMapper.showCompanies();
            Integer assetsAdminId = 0;
            for (Company company : companies) {
                if (company.getCompanyName().equals(fcDTO.getChangeBelongCompNew())){
                    assetsAdminId = company.getCompanyId();
                }
            }
            AssetFinanceChange assetChange = afcMapper.selectByChangeNo(fcDTO.getChangeNo());
            System.out.println(assetChange);
            String assetIds = assetChange.getAssetIds();
            System.out.println(assetIds);
            String[] ids = assetIds.split(",");
            for (String id : ids) {
                Map map = new HashMap();
                map.put("assetsId",Integer.valueOf(id));
                map.put("assetsAdminId",assetsAdminId);
                map.put("assetsMoney",fcDTO.getAssetMoneyNew());
                afcMapper.updateAssetsByFinanceChange(map);
            }
        }

        R r = new R();
        if (row > 0) {
            r.setCode(200);
            r.setMsg("success");
        } else {
            r.setCode(500);
            r.setMsg("更新失败");
        }
        return r;
    }

    /*
     *
     * @description:删除财务信息变更
     * @author: jianjiawei
     * @date: 2023/2/21 19:20
     **/
    @Override
    public R deleteAssetsFinanceChange(Integer id) {
        int row = afcMapper.deleteFinanceChangeById(id);
        R r = new R();
        if (row > 0) {
            r.setCode(200);
            r.setMsg("success");
        } else {
            r.setCode(500);
            r.setMsg("删除失败");
        }
        return r;
    }

    /*
    *
     * @description:根据高级搜索查询财务变更信息数据
     * @author: jianjiawei
     * @date: 2023/2/22 8:54
     **/
    @Override
    public R findFCListByAdv(AssetFCAdvDTO advDTO) {

        PageHelper.startPage(advDTO.getCurrentPage(),advDTO.getPageSize());
        List<AssetFCVO> assetFCVOS = afcMapper.selectAssetFinanceChangeByAdv(advDTO);
        R r = new R();
        if (assetFCVOS != null & assetFCVOS.size() > 0){
            String data = JSONObject.toJSONString(assetFCVOS);
            r.setCode(200);
            r.setMsg("success");
            r.setData(data);
        }else {
            r.setCode(200);
            r.setMsg("没有满足条件的数据");
        }
        return r;
    }

    @Override
    public HSSFWorkbook getWorkbook() {
        String[] header = {"主键ID","变更单号","变更状态","业务日期","处理人","资产名称","业务所属公司","所属公司变更为","资产金额变更为","资产id列表","备注"};
        String[] properties = {"id","changeNo","changeState","changeDate","changeHandler","assetName","changeBelongcomp","AssetBelongcompNew","assetMoneyNew","assetIds","remarks"};
        List assetFinanceChanges = afcMapper.selectAll();
        List<AssetFinanceChange> assetFinanceChanges1 = afcMapper.selectAll();
        for (AssetFinanceChange assetFinanceChange : assetFinanceChanges1) {
            System.out.println(assetFinanceChange);
        }
        ExcelExportHelper excelHelper = new ExcelExportHelper();
        HSSFWorkbook workbook = excelHelper.exportExcel(header, properties, assetFinanceChanges, "财务信息变更");
        return workbook;
    }

    public List<FCAssetsVO> showChangeAssetsList(Integer fcId) {
        AssetUpdateFCVO updateFCVO = afcMapper.selectAssetFinanceChangeById(fcId);
        String assetIds = updateFCVO.getAssetIds();
        List<FCAssetsVO> listVO = new ArrayList<>();
        if (assetIds != null & assetIds != "") {
            String[] assetsIds = assetIds.split(",");
            for (String assetsId : assetsIds) {
                FCAssetsVO fcAssetsVO = afcMapper.selectAssetsById(Long.parseLong(assetsId));
                fcAssetsVO.setAssetsAdmin(acMapper.getAssetsAdmin(fcAssetsVO.getAssetsId()));
                listVO.add(fcAssetsVO);
            }
        }
        return listVO;
    }

}
